package webgamemaker.Editor;

import com.smartgwt.client.types.SelectionType;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class Toolbar extends ToolStrip 
{
	public ToolStripButton undoButton, redoButton;
	public ToolStripButton layerButton[], allLayerButton;
	public ClickHandler historyHandler, layerHandler;
	
	public Toolbar()
	{
		historyHandler = new ClickHandler()
			{
				public void onClick(ClickEvent event) 
				{
					if(event.getSource() == undoButton)
						ProjectManager.getInstance().data.historyManager.undo();
					else ProjectManager.getInstance().data.historyManager.redo();
				}

			};
		undoButton = new ToolStripButton();
		undoButton.setIcon("../data/icons/back.png");
		undoButton.addClickHandler(historyHandler);
		addButton(undoButton);
		redoButton = new ToolStripButton();
		redoButton.setIcon("../data/icons/next.png");
		redoButton.addClickHandler(historyHandler);
		addButton(redoButton);
		
		addSeparator();
		
		layerHandler = new ClickHandler()
		{
			public void onClick(ClickEvent event) 
			{
				if(event.getSource() == allLayerButton)
				{
					ProjectManager.getInstance().currentLayer = -1;
				}
				else 
				{
					int index = 0;
					for(int i = 0; i < layerButton.length; i++)
						if(event.getSource() == layerButton[i])
							index = i;
					System.out.println("index = "+index);
					ProjectManager.getInstance().currentLayer = index;
				}
			}
		};
		
		allLayerButton = new ToolStripButton();
		allLayerButton.setTitle("all");
		allLayerButton.setActionType(SelectionType.RADIO);
		allLayerButton.setRadioGroup("layerButton");
		allLayerButton.addClickHandler(layerHandler);
		addButton(allLayerButton);
		
		//makeLayerButton(3);
	}
	
	public void makeLayerButton(int num_layer)
	{
		ProjectManager.getInstance().currentLayer = -1;
		if(layerButton != null)
			for(int i = 0; i < layerButton.length; i++)
				removeMember(layerButton[i]);
		layerButton = new ToolStripButton[num_layer];
		for(int i = 0; i < layerButton.length; i++)
		{
			layerButton[i] = new ToolStripButton();
			layerButton[i].setTitle(""+i);
			layerButton[i].setActionType(SelectionType.RADIO);
			layerButton[i].setRadioGroup("layerButton");
			layerButton[i].addClickHandler(layerHandler);
			addButton(layerButton[i], getMemberNumber(allLayerButton));
		}
	}
}
